var exports = {};

var textView = require("./../view/textView.js");
var bgView = require("./../view/bgView.js");
var operView = require("./../view/operView.js");
var operationView = require("./../view/operationView.js");
var layerView = require("./../view/layerView.js");


var productView = require("./../view/productView.js");
var imgView = require("./../view/imgView.js");

exports.text = textView;
exports.bg = bgView;
exports.oper = operView;
exports.layer = layerView;
exports.img = imgView;

var currView = null;

/*
 * 初始化
 */
exports.init = function(){
	bindEvent();
	
	textView.init();
	bgView.init();
	operView.init();
	operationView.init();
	
	productView.init();
	imgView.init();
	
	if(config_gl.apiPath.indexOf("http://127.0.0.1:8086/api") != -1){
		getKaptch();
	}
}

exports.bind = function(ele){
	if(currView){
		currView.unbind();
		currView = null;
	}
	var type = ele.type;
	switch (type){
		case "text":
			currView = exports.text;
			break;
		case "bg":
			currView = exports.bg;
			break;
		default:
			break;
	}
	if(currView){
		currView.bind(ele);
	}
	operView.bind(ele);
}

exports.unbind = function(){
	if(currView){
		currView.unbind();
		currView = null;
	}
}

exports.updateLayer = function(){
	layerView.update();
}

/*
 * 绑定事件
 */
function bindEvent(){
	$(".productSave").on("click", saveHandler);
	
	$(".left_box .title .icon-guanbi").on("click", hideLeftBox);
}


function hideLeftBox(){
	$(this).parents(".left_box").fadeOut();
}

/*
 * 保存
 */
function saveHandler(e){
	var json = Design.mainEditer.getSaveData();
	console.log(JSON.stringify(json));
}

module.exports = exports;
